<?php

class Page {

	/**
	 * 默认配置
	*/
	private $config=array(
		'nowPage'   =>1,//当前页数
		'listRows'  =>10,//每页显示行数
		'totalRows' =>'',//总行数
		'totalPages'=>'',//总页数
		'firstRow'  =>'',//起始行数
		'rollPage'  =>'',//分页栏每页显示的页数
		'coolPages' =>'',//分页的栏的总页数
		'pageName'  =>'page',//分页参数名称
		'pageStyle'  =>'tencent',//默认分页参数名称
		'parameter' =>array(),//页数跳转时要带的参数
	);

	/**
	 * 构造函数
	 * @param array $totalRows 总行数
	 * @param array $listRows 每页显示行数
	 * @param array $parameter 页数跳转时要带的参数
	 * @return void
	*/
	public function __construct($totalRows,$listRows=0,$parameter=array()){
		//合并配置
		$config=$this->config;
		$config['totalRows']=$totalRows;
		$config['listRows']=$listRows>0?$listRows:$config['listRows'];
		$config['parameter']=is_array($parameter)?$parameter:array();
		$config['nowPage']=empty($_GET[$config['pageName']])?$config['nowPage']:$_GET[$config['pageName']];
		$config['totalPages']=ceil($config['totalRows']/$config['listRows']);
		$config['firstRow']=($config['nowPage']-1)*$config['listRows'];
		$this->config=$config;
		//兼容老版本
		$this->firstRow=$config['firstRow'];
		$this->listRows=$config['listRows'];
	}

	/**
	 * 结果限制
	*/
	public function limit(){
		return $this->config['firstRow'].','.$this->config['listRows'];
	}

	/**
	 * 显示分页
	 * @style string 分页样式
	 * @return string
	*/
	public function show($style=''){
		$style=empty($style)?$this->config['pageStyle']:$style;
		if($this->config['nowPage']<1||$this->config['nowPage']>$this->config['totalPages']){
			return '';
		}else if(!method_exists($this,$style)){
			return '';
		}else{
			return $this->$style();
		}
	}

	/**
	 * 分页样式（九宫格）
	 * @return string
	*/
	private function nine(){
		return $this->lattice(4);
	}

	/**
	 * 分页样式（格子）
	 * @param $mod int 两边长度
	 * @return string
	*/
	private function tencent($mod=3){
		$config=$this->config;
		//分页长度
		$length=$mod*2+1;
		//分页结束
		$end=$config['nowPage']+$mod;
		$end=$end<$length?$length:$end;
		$end=$length>$config['totalPages']?$config['totalPages']:$end;
		$end=$end>$config['totalPages']?$config['totalPages']:$end;
		//分页开始
		$begin=$config['nowPage']-$mod;
		$begin=$end-$config['nowPage']<$mod?$end-$length+1:$begin;
		$begin=$begin<1?1:$begin;
		//是否显示首页
		$first=$begin>1;
		//是否显示总页
		$total=$config['totalPages']-$length>=$begin;
		//上一页
		$html='<a '.($config['nowPage']>1?'href="'.$this->url($config['nowPage']-1).'"':'').' class="prev'.($config['nowPage']>1?'':' disabled').'">上一页</a>';
		$html.=$first?'<a href="'.$this->url(1).'">1</a><a class="more">...</a>':'';
		//九宫格
		for(;$begin<=$end;$begin++){
			$html.='<a '.($config['nowPage']!=$begin?'href="'.$this->url($begin).'"':'class="selected"').'>'.$begin.'</a>';
		}
		//总页数
		$html.=$total?'<a class="more">...</a><a href="'.$this->url($config['totalPages']).'">'.$config['totalPages'].'</a>':'';
		//下一页
		$html.='<a '.($config['nowPage']<$config['totalPages']?'href="'.$this->url($config['nowPage']+1).'"':'').' class="next'.($config['nowPage']<$config['totalPages']?'':' disabled').'">下一页</a>';
		$html.='<a class="total">共 '.$config['totalRows'].' 条</a>';
		return $html;
	}

	/**
	 * 分页样式（格子）
	 * @param $mod int 两边长度
	 * @return string
	*/
	private function lattice($mod=3){
		$config=$this->config;
		//分页长度
		$length=$mod*2+1;
		//分页结束
		$end=$config['nowPage']+$mod;
		$end=$end<$length?$length:$end;
		$end=$length>$config['totalPages']?$config['totalPages']:$end;
		$end=$end>$config['totalPages']?$config['totalPages']:$end;
		//分页开始
		$begin=$config['nowPage']-$mod;
		$begin=$end-$config['nowPage']<$mod?$end-$length+1:$begin;
		$begin=$begin<1?1:$begin;
		//是否显示首页
		$first=$begin>1;
		//是否显示总页
		$total=$config['totalPages']-$length>=$begin;
		//上一页
		$html='<a '.($config['nowPage']>1?'href="'.$this->url($config['nowPage']-1).'"':'').' class="prev'.($config['nowPage']>1?'':' disabled').'"><</a>';
		$html.=$first?'<a href="'.$this->url(1).'">1</a>':'';
		//九宫格
		for(;$begin<=$end;$begin++){
			$html.='<a '.($config['nowPage']!=$begin?'href="'.$this->url($begin).'"':'class="selected"').'>'.$begin.'</a>';
		}
		//总页数
		$html.=$total?'<a href="'.$this->url($config['totalPages']).'">'.$config['totalPages'].'</a>':'';
		//下一页
		$html.='<a '.($config['nowPage']<$config['totalPages']?'href="'.$this->url($config['nowPage']+1).'"':'').' class="next'.($config['nowPage']<$config['totalPages']?'':' disabled').'">></a>';
		return $html;
	}

	/**
	 * 分页样式（简单）
	 * @return string
	*/
	private function simple(){
		$config=$this->config;
		$html ='<a '.($config['nowPage']>1?'href="'.$this->url(1).'"':'').'>首页</a>';
		$html.='<a '.($config['nowPage']>1?'href="'.$this->url($config['nowPage']-1).'"':'').'>上一页</a>';
		$html.='<a '.($config['nowPage']<$config['totalPages']?'href="'.$this->url($config['nowPage']+1).'"':'').'>下一页</a>';
		$html.='<a '.($config['nowPage']<$config['totalPages']?'href="'.$this->url($config['totalPages']).'"':'').'>尾页</a>';
		$html.='共 '.$config['totalRows'].' 条记录 '.$config['nowPage'].'/'.$config['totalPages'].' 页';
		return $html;
	}

	/**
	 * 分页地址
	 * @param int $page 页数
	 * @return string
	*/
	private function url($page){
		$url=parse_url($_SERVER['REQUEST_URI']);
		parse_str($url['query'],$param);
		unset($param[$this->config['pageName']]);
		$param[$this->config['pageName']]=$page;
		return $url['path'].'?'.http_build_query($param);
	}

}

?>